package me.yxy.mobanddd.domain.warehouse;


import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.Date;

public interface WarehouseRepository extends PagingAndSortingRepository<Warehouse,Long> {


    /**
     * 分组统计每个仓库当天运输单量
     *
     * <p> 这里依旧用的是HQL，返回通过new View(param)的方式来构建返回Bean</p>
     * <p> 只要确认sql能够通过hql写出来，那这样做是可以的</p>
     *
     * @param date
     * @param pageable
     * @return
     */
    @Query(value="SELECT new me.yxy.mobanddd.finder.view.warehouse.WarehouseConsoleGroupView(" +
            "tod.warehouse.name,count(1) " +
            ") FROM TransportOrder tod " +
            "WHERE DATE(tod.createdTime) = ?1 " +
            "GROUP BY tod.warehouse.name "
    )
    Page<Warehouse> groupTransportCount(Date date, Pageable pageable);

    @Query(value="SELECT new map(" +
            "tod.warehouse.name as name,count(1) as cnt " +
            ") FROM TransportOrder tod " +
            "WHERE DATE(tod.createdTime) = ?1 " +
            "GROUP BY tod.warehouse.name "
    )
    Page groupTransportCountToMap(Date date, Pageable pageable);

    @Query(value="SELECT * " +
            "FROM  t_warehouse ",
            nativeQuery = true
    )
    Page<Warehouse> nativeQUery(String date, Pageable pageable);
}
